/// <reference path="../../../assets/jsplugin/core.js" />
/// <reference path="../../../assets/jsplugin/jquery-1.11.1.min.js" />

(function () {
    var menuList = $("#menuList").html();
    var $ngMenu = $("#ngMenu");
    var $vehicleMsgList = $("#vehicle-msg-list");
    var $baocheMsgList = $("#baoche-msg-list");
    var vehicleMsgHtml = undefined;
    var $driverMsgList = $("#driver-msg-list");
    var driverMsgHtml = undefined;
    var _this = {
        test: function (e) { 
            App["viewObj"] = e;
        },
        changeView: function (obj) {
            var url = (obj.url||"/index/main").split("\?")[0];
            var _url = url + "_tpl.html";
            var _opt = {
                url: App.pathCfg.skin + App.pathCfg.tplURI + _url,
                callback: _this.test, 
                ux: App.pathCfg.skin + "ux" + url + ".js",
            };
           App.closeEntitySlider();
           App.drawViewPanel(_opt);
        },
        showNav: function (nav) {
            $.each(nav || [], function (i, el) {
                var $a = $("<a nav-click>");
                $a.addClass("item");
                $a.text(el.name);
                $a.attr("vp-href", el.url);
                $a.data(el.menu);
                $a.attr({id:el.name});
                $ngMenu.append($a);
            });
        },
        showMenu: function (data) {
            var hash = window.location.hash.replace(/^#vi=/gi, "");
            $("#menu .openable").remove();
            $.each(data || [], function (i, el) {
                if (typeof (el) === 'function') return;
                var $menuList = $(menuList);
                $menuList.setHtml(el);
                if(!!hash && el.url==hash){
                    $menuList.addClass("open");
                }
                var $a = $menuList.find("a");
                // 判断包车,跳转
                if( (el.url != undefined) && (el.url.indexOf('baoche') == 1) ){
                	$a.attr("href", window.location.origin + App.rootCfg.ROOT + "/page/index.html#vi=" + el.url );
//              	$a.attr("href", "http://localhost:8000/src/page/index.html#vi=" + el.url );
                }else{
                	$a.attr("href", window.location.origin + "/admin/page/index.html#vi=" + el.url );
//              	$a.attr("href", "http://localhost:9999/admin/page/index.html#vi=" + el.url );
                }
                $a.data(el);
                $("#menu").append($menuList)
            });
        },
        loadUrlHashToView: function () {
            var _vi = App.getHash("vi");
            var _opt = {
                url:_vi
            } 
            _this.changeView(_opt)
        },
        loadUesr: function (fn) {
            App.ajax({
                data: {},
                resUrl: "/user/login",
                callback: function (rb) {
                    App.user = rb.data;
                    var mch = App.user.mch;
                    if(!!mch){
                        var mchName = mch.name;
                        if(!!App.user.company){
                            mchName = mchName+"."+App.user.company.name;
                        }
                        if(!!App.user.pheasant){
                            mchName = mchName+"."+App.user.pheasant.name;
                        }
                        $("#platform-name").html(mchName);
                    }
                    $("#user-name").html(App.user.name);
                    require(['menu'], function () {
                        _this.showNav(App.menu);
                        !!fn && fn();
                    });
                }
            });
        },
        loadSysMsg: function (fn) {
            App.ajax({
                data: {},
                resUrl: "/user/sysmsg",
                callback: function (rb) {
                    if (!rb.status) {
                        return;
                    }
                    var vehicles = rb.data.vehicles;
                    var drivers = rb.data.drivers;
                    $("#vehicle-msg-count").html(vehicles.length);
                    $("#driver-msg-count").html(drivers.length);

                    $.each(vehicles, function(i, vehicle) {
                       var $msg = $(vehicleMsgHtml);
                        $msg.data(vehicle);
                        $msg.setHtml(vehicle);
                        $vehicleMsgList.append($msg);
                    });
                    $.each(drivers, function(i, driver) {
                       var $msg = $(vehicleMsgHtml);
                        $msg.data(driver);
                        $msg.setHtml(driver);
                        $driverMsgList.append($msg);
                    });
                    
                }
            });
            App.ajax({
                data: {},
                resUrl: "/user/sysmsg",
                callback: function (rb) {
                    if (!rb.status) {
                        return;
                    }
                    var baoche = rb.data.vehicles;
                    $("#baoche-msg-count").html(baoche.length);
                    $.each(baoche, function(i, vehicle) {
                    	if(i==5){
                    		$baocheMsgList.append("<a href='http://localhost:8000/src/page/index.html#vi=/baoche/invoice' style='margin:0 auto;display:block;height:30px;width:70px;'><button class='btn btn-sm btn-success' style='line-height:17px'>查看更多</button></a>");
                    		
                    	};
                    	if(i<5){
                    		var $msg = $(baocheMsgHtml);
	                        $msg.data(vehicle);
	                        $msg.setHtml(vehicle);
	                        $baocheMsgList.append($msg);
                    	}
                    });
                }
            });
        },
        checkExtNull : function($this){
            var keys = {};
            $this.find("input,select,textarea").each(function(){
                var $input = $(this);
                var name = $input.attr("name");
                keys[name] = name;
            });
            var ok = 0;
            var unok = 0;
            $.each(keys, function(key, name){
                var $input = $this.find("[name="+name+"]");
                var type = $input.attr("type");
                var value = undefined;
                if(type=="hidden") return;
                if(type=="radio" || type=="checkbox"){
                    value = $this.find("[name="+name+"]:checked").val();
                }else if(type=="file"){
                    var $img = $input.parents(".smart-widget-body:eq(0)").find("img");
                    if(!!$img){
                        var img = new Image();
                        img.src = $img.attr("uri");
                        if(img.complete) {
                            value = $img.attr("uri");
                        }
                    }
                }else{
                    value = $input.val();
                }
                if(!!value){
                    ok++;
                }else{
                    unok++;
                }
            });
            $this.find(".smart-widget-header .tip-count .badge-success").html(ok);
            $this.find(".smart-widget-header .tip-count .badge-danger").html(unok);
        },
        initEvent: function () {
        	setTimeout(function(){
        		App.ajax({
					data: {
						
					},
					resUrl: "ygxLineStation/add",
					callback: function(rb) {
						if(rb.code == 0) {
							
						} else {
							
						}
					}
				});//需要返回值来显示
        		$("#msgBox").show();
        	},2000)
        	$("#closeMsgBox").on('click',function(){
        		$("#msgBox").hide();
        	})
        	$("#titDetalisBtu").on('click',function(){
        		//根据ID进入详情页面
        	})
            $("body").on("click", "[nav-click]", function (event) {
                //当展开编辑页，头顶的菜单也不能点击
                if($("#entitySlider").hasClass("in")) return;

                _this.showMenu($(event.target).data());
                var $this = $(this);
                $this.siblings().removeClass("active");
                $this.addClass("active");
            }).on("click", "[vi-click]", function (event) {
                var $this = $(this);
                $this.siblings().removeClass("open");
                $this.addClass("open");
                var _opt = $this.find("a").data();
                _opt && _opt.url && _this.changeView(_opt);
            }).delegate("[data-toggle=hydra-collapse]", "click", function () {
                var data = $(this).data();
                if(data.form && /add/.test(data.form)){
                    $(data.form)[0] && $(data.form)[0].reset();
                }
                $(data.target).slideToggle();
            }).delegate(".module-detail-view .smart-widget-header, .form-group .smart-widget-header", "click", function(){
                var $activeWidget = $(this).parent();
                $activeWidget.find('.smart-widget-inner').slideToggle();
                $activeWidget.toggleClass('smart-widget-collapsed');
                var $activeSpinIcon = $activeWidget.find('.refresh-icon-animated').fadeIn();
                setTimeout(function() {
                    $activeSpinIcon.fadeOut();
                },500);
                $activeWidget = '';
                return false;
            }).delegate("#search-form [type=submit]", "click", function(){
                $("#search-div").slideUp();
                $(".main-container").scrollTop(0);
            }).delegate(".smart-widget-header .widget-refresh-option", "click", function(){
                _this.loadUrlHashToView();
                return false;
            });
            $("#logout").on("click", function () {
                App.ajax({
                    resUrl: "/user/logout",
                    callback: function (rb) {
                        window.location.href = 'login.html'
                    }
                });
            });
            $("body").undelegate("#baoche-msg-list button", 'click');
            $("body").delegate("#baoche-msg-list button", 'click',function(){
            	var msg = '订单内容...........';
            	var btnok = '前往订单！'
            	var _index = layer.confirm('<div style="margin: auto;width: 222px;">'+msg+'</div>', {
					type:1,
					title:"查看订单",
					area: ['400px', 'auto'], //宽高
					btn: [btnok,]
				}, function(){
                        
				}, function(){
					// 取消
					layer.close(_index);
				});
            });
        },
        initHash: function(){
            var hash = App.getHash("vi");
            if(!hash) return;

            hash = window.location.hash.replace("#vi=", "");//考虑有参数的菜单
            var thisNav = undefined;
            $.each(App.menu, function(i, nav){
                $.each(nav.menu || [], function (j, menu) {
                    if(menu.url==hash){
                        thisNav = nav;
                        return false;
                    };
                });
            });
            if(!thisNav) return;

            _this.showMenu(thisNav.menu);
            $("#" + thisNav.name).addClass("active");
        },
        init: function () {
            _this.loadUesr(function () {
                _this.initHash();
                _this.initEvent();
                _this.loadUrlHashToView();

                vehicleMsgHtml = $vehicleMsgList.html();
                baocheMsgHtml = $baocheMsgList.html();
                $vehicleMsgList.empty();
                $baocheMsgList.empty();
                driverMsgHtml = $driverMsgList.html();
                $driverMsgList.empty();
                if(App.user.type==2||App.user.type==3){
                    _this.loadSysMsg();
                }
            });
        }
    }
    Page = _this; //页面初始化入口设置
})();
